/**
 * maoyu
 * 2014.5.24
 */
package com.example.tools;

import java.io.File;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.os.Environment;

public class ImageUtils {
	/** 请求相册 */
	public static final int REQUEST_CODE_GETIMAGE_BYSDCARD = 20;
	/** 请求相机 */
	public static final int REQUEST_CODE_GETIMAGE_BYCAMERA = 21;
	public static int mImageWidthScale;// 获取截图时的图片宽的比值
	public static int mImageHeigthScale;// 获取截图时的图片高的比值
	public static long millionSeconds;// 毫秒

	// public static boolean mIsSquare;

	/**
	 * 获取照相机使用的目录,并创建目录
	 * 
	 * @return
	 */
	public static String getCamerPath() {
		String path = Environment.getExternalStorageDirectory()
				+ File.separator + "MallPhotos" + File.separator;

		File filePath = new File(path);
		if (!filePath.exists()) {
			filePath.mkdir();
		}

		return path;
	}

	/**
	 * 使用当前时间戳拼接一个唯一的文件名
	 * 
	 * @param format
	 * @return
	 */
	public static String getTempFileName() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss_SS");
		String fileName = format.format(new Timestamp(System
				.currentTimeMillis()));
		return fileName;
	}

	/**
	 * 缓存图片
	 */
	public static DisplayImageOptions options = new DisplayImageOptions.Builder()
			.cacheOnDisc(true).considerExifParams(true)
			.bitmapConfig(Bitmap.Config.RGB_565).build();
	public static ImageLoader mImageLoader;

	public static ImageLoader imgLoader(Context context) {
		mImageLoader = ImageLoader.getInstance();
		mImageLoader.init(ImageLoaderConfiguration.createDefault(context));
		return mImageLoader;

	}

	/**
	 * 将标准的时间格式转化为毫秒
	 */
	@SuppressLint("SimpleDateFormat")
	public static long transformTime(String time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		try {
			millionSeconds = sdf.parse(time).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}// 毫秒
		return millionSeconds;
	}

	/**
	 * 图片裁剪
	 * 
	 * @param bgimage
	 * @param newWidth
	 * @param newHeight
	 * @return
	 */
	public static Bitmap zoomImage(Bitmap bgimage, double newWidth,
			double newHeight) {
		// 获取这个图片的宽和高
		float width = bgimage.getWidth();
		float height = bgimage.getHeight();
		// 创建操作图片用的matrix对象
		Matrix matrix = new Matrix();
		// 计算宽高缩放率
		float scaleWidth = ((float) newWidth) / width;
		float scaleHeight = ((float) newHeight) / height;
		// 缩放图片动作
		matrix.postScale(scaleWidth, scaleHeight);
		Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, (int) width,
				(int) height, matrix, true);
		return bitmap;
	}

	public static Bitmap small(Bitmap bitmap) {
		Matrix matrix = new Matrix();
		matrix.postScale(0.8f, 0.8f); // 长和宽放大缩小的比例
		Bitmap resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
				bitmap.getHeight(), matrix, true);
		int bmpSize = resizeBmp.getRowBytes() * resizeBmp.getHeight();
		if (bmpSize / 1024 > 32) {
			return small(resizeBmp);
		}
		return resizeBmp;
	}
}
